﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.Instructor
{
    public partial class Ins_Examination_Del : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[2].Selected = true;

            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            // Get the id from querystring
            id = (string)Session["userName"];

            if (id == null)
                Response.Redirect("../login.aspx");

            string strSQL = " SELECT E.exId AS \"ExamId\", E.e_startTime AS \"Start Time-Date\", E.e_endTime AS \"End Time-Date\", E.e_type AS \"Type\" " +
                            " FROM Examination E ";
                           

            sqlCommand = new MySqlCommand(strSQL, conn);
            readVals = sqlCommand.ExecuteReader();

            if (!IsPostBack)
            {
                MainTable.DataSource = readVals;
                MainTable.DataBind();
            }
        }

        protected void delete_clicked(object sender, EventArgs e)
        {
            GridViewRow clickedRow = ((Button)sender).NamingContainer as GridViewRow;

            string exId = clickedRow.Cells[1].Text;

            // Connect to DB and delete that row.
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string strSQL = " DELETE FROM Examination " +
                            " WHERE exId =" + exId;

            sqlCommand = new MySqlCommand(strSQL, conn);
            sqlCommand.ExecuteNonQuery();

            conn.Close();

            Response.Redirect("Ins_Examination_Del.aspx");
        }
    }
}